package com.ms.service.module.fish.game;

import java.io.Serializable;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.ms.service.module.fish.utils.GoConstant;

public class FishDesk implements Serializable{

	private static final long serialVersionUID = 1L;
	
	private static Logger logger = LoggerFactory.getLogger(FishDesk.class);

	/**
	 * 计数器
	 */
	public static AtomicInteger counter = new AtomicInteger();
	
//	private int id = counter.incrementAndGet();
	private int id ;
	
	private GameUser[] users = new GameUser[GoConstant.CHAIR_SIZE];
	
	private KeepSeet[] keepSeets = new KeepSeet[GoConstant.CHAIR_SIZE];
	
	private long[] preIndexs = new long[GoConstant.CHAIR_SIZE];
	
	private FishGame game ;
	
	private int msize; // 房间里的玩家数

	private byte type;
	
	private List<Rank> ranks = Lists.newArrayList(); // 大奖
	
	public FishDesk(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public GameUser[] getUsers() {
		return users;
	}
	
	public int getMsize() {
		return msize;
	}

	public boolean addUser(GameUser user,int index) {
		if(!checkEnter(user, index)){
			return false;
		}
		this.preIndexs[index] = 0;
		this.users[index] = user;
		user.setDeskIndex(index);
		msize++;
		user.setDesk(this);
		return true;
	}
	
	private boolean checkEnter(GameUser user,int index) {
		if(this.users[index] != null)return false;
		if(this.keepSeets[index] != null){
			if(this.keepSeets[index].isOver() || this.keepSeets[index].getUid().equals(user.getId())) {
				keepSeets[index] = null;
			}else{
				logger.error("该座位被其他玩家保留！");
				return false;
			}
		}
		return true;
	}
	
	public void logout(GameUser user,int action){
		user.setDesk(null);
		if(user.getDeskIndex() >= 0) {
			this.users[user.getDeskIndex()] = null;
			if(action == 1){
				// test
				long endTime = System.currentTimeMillis()/1000 + (10*60);
				KeepSeet seet = new KeepSeet(id,user.getDeskIndex(),user.getId(), endTime);
				this.keepSeets[user.getDeskIndex()] = seet;
				user.addKeepSeat(seet);
			}
			user.setDeskIndex(-1);
			msize--;
			if(msize == 0) {
				// 所有人员推出，结束游戏
				gamePause();
			}
		}
	}

	public boolean enterGame(GameUser gameUser) {
		// 创建游戏
		boolean isNew = false;
		if(game == null){
			game = new FishGame(this);
			isNew = true;
		}else if(msize == 1){
			game.continun();
		}
		gameUser.setGame(game);
		return isNew;
	}
	
	public FishGame getGame(){
		return game;
	}

	public GameUser getUser(int index) {
		if(index < users.length){
			return users[index];
		}
		return null;
	}

	public void gamePause(){
		if(game != null){
			game.gamePause();
		}
//		this.game = null;
	}

	public byte getType() {
		return type;
	}
	
	public void setType(byte type){
		this.type = type;
	}
	
	public int getRate(){
		return GoConstant.ROOM_RATE[type];
	}
	
	public void clearKeepSeat() {
		for(int i=0;i<keepSeets.length;i++){
			if(keepSeets[i] != null && keepSeets[i].isOver()){
				keepSeets[i] = null;
			}
		}
	}

	public KeepSeet keepSeat(int i) {
		return keepSeets[i];
	}
	
	public int keepSize() {
		int count = 0;
		for(KeepSeet keep:keepSeets){
			if(keep != null){
				count++;
			}
		}
		return count;
	}
	
	public void removeKeepSeat(KeepSeet kp) {
		keepSeets[kp.getIndex()] = null;
	}
	
	public boolean preEnter(GameUser user,int index){
		if(!checkEnter(user, index)) return false;
		clearPreIndex();
		if(this.preIndexs[index] != 0)return false;
		preIndexs[index] = System.currentTimeMillis();
		return true;
	}
	
	public void clearPreIndex(){
		long now = System.currentTimeMillis();
		for(int i=0;i<preIndexs.length;i++){
			boolean b = (preIndexs[i] != 0) && (now - preIndexs[i] > 30000);
			if(b) preIndexs[i]  = 0;
		}
	}
	
	public List<Rank> getRanks() {
		return ranks;
	}
	
	public void addRank(Rank rank) {
		while(ranks.size() >= 10){
			ranks.remove(0);
		}
		ranks.add(rank);
	}
	
}
